home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 19 / CU Amiga Magazine's Super CD-ROM 19 (1998)(EMAP Images)(GB)[!][issue 1998-02].iso / CUCD / Programming / LEDA / incl / LEDA.020+881 / dictionary.h < prev    next >
C/C++ Source or Header  |  1994-08-05  |  5KB  |  150 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  dictionary.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_DICTIONARY_H
  16. #define LEDA_DICTIONARY_H
  17.  
  18. #include <LEDA/basic.h>    
  19.  
  20.  
  21. #define DIC_DEF_IMPL skiplist
  22. #include <LEDA/impl/skiplist.h> 
  23. typedef skiplist_item dic_item;
  24.  
  25.  
  26.  
  27. template <class ktype, class itype> 
  28.  
  29. class _CLASSTYPE dictionary : public virtual DIC_DEF_IMPL 
  30. {
  31.  
  32. int  int_type()              const { return INT_TYPE(ktype); }
  33. int  cmp(GenPtr x, GenPtr y) const
  34.                           { return compare(ACCESS(ktype,x),ACCESS(ktype,y)); }
  35. void clear_key(GenPtr& x) const { Clear(ACCESS(ktype,x)); }
  36. void clear_inf(GenPtr& x) const { Clear(ACCESS(itype,x)); }
  37. void copy_key(GenPtr& x)  const { x=Copy(ACCESS(ktype,x)); }
  38. void copy_inf(GenPtr& x)  const { x=Copy(ACCESS(itype,x)); }
  39.  
  40. public:
  41.  
  42. virtual dic_item lookup(ktype x) const
  43. { return DIC_DEF_IMPL::lookup(Convert(x));}
  44.  
  45. virtual int  defined(ktype x) const 
  46. { return (lookup(x) == nil) ? false : true; }
  47.  
  48. virtual void change_inf(dic_item it, itype i)
  49. { DIC_DEF_IMPL::change_inf(it,Convert(i)); }
  50.  
  51. virtual dic_item insert(ktype x,itype y)
  52. { return DIC_DEF_IMPL::insert(Convert(x),Convert(y)); } 
  53.  
  54. virtual void  del(ktype x)          { DIC_DEF_IMPL::del(Convert(x)); } 
  55. virtual void  del_item(dic_item it) { DIC_DEF_IMPL::del_item(it); } 
  56.  
  57. virtual ktype key(dic_item it) const { return ACCESS(ktype,DIC_DEF_IMPL::key(it));}
  58. virtual itype inf(dic_item it) const { return ACCESS(itype,DIC_DEF_IMPL::inf(it));}
  59. virtual itype access(ktype k)  const { return inf(lookup(k));}
  60.  
  61. virtual int      size()  const { return DIC_DEF_IMPL::size(); }
  62. virtual bool     empty() const { return (size()==0) ? true : false; }
  63. virtual void     clear() { DIC_DEF_IMPL::clear(); }
  64. virtual dic_item first_item() const { return DIC_DEF_IMPL::first_item(); }
  65. virtual dic_item next_item(dic_item it) const { return DIC_DEF_IMPL::next_item(it);}
  66.  
  67. dictionary<ktype,itype>& operator=(const dictionary<ktype,itype>& D)
  68. { DIC_DEF_IMPL::operator=(D); return *this; }
  69.          
  70. dictionary() {}
  71.  
  72. dictionary(const dictionary<ktype,itype>& D) : DIC_DEF_IMPL(D) {}
  73.  
  74. virtual ~dictionary()   { DIC_DEF_IMPL::clear(); }
  75. };
  76.  
  77.  
  78. //------------------------------------------------------------------------------
  79. //
  80. // Dictionaries with implementation parameter:
  81. //
  82. //   _dictionary<keytype,inftype,dic_impl> 
  83. //
  84. //------------------------------------------------------------------------------
  85.  
  86. #define _dictionary_class(ktype,itype,impl)\
  87. \
  88. class _CLASSTYPE _dict_class_(ktype,itype,impl) :private virtual impl,\
  89.                                                  public dictionary<ktype,itype>\
  90. {\
  91. \
  92. int int_type() const { return INT_TYPE(ktype); }\
  93. \
  94. int cmp(GenPtr x, GenPtr y) const\
  95. { return compare(ACCESS(ktype,x),ACCESS(ktype,y)); }\
  96. void clear_key(GenPtr& x) const { Clear(ACCESS(ktype,x)); }\
  97. void clear_inf(GenPtr& x) const { Clear(ACCESS(itype,x)); }\
  98. void copy_key(GenPtr& x) const { x=Copy(ACCESS(ktype,x)); }\
  99. void copy_inf(GenPtr& x) const { x=Copy(ACCESS(itype,x)); }\
  100. void print_key(GenPtr x) const { Print(ACCESS(ktype,x),cout); }\
  101. void print_inf(GenPtr x) const { Print(ACCESS(itype,x),cout); }\
  102. \
  103. public:\
  104. \
  105. dic_item lookup(ktype x) const { return dic_item(impl::lookup(Convert(x))); }\
  106. \
  107. int defined(ktype x) const { return (lookup(x)==nil) ? false : true; }\
  108. \
  109. void change_inf(dic_item it, itype i)\
  110. { impl::change_inf(impl::item(it),Convert(i));}\
  111. \
  112. dic_item insert(ktype x,itype y)\
  113. { return dic_item(impl::insert(Convert(x),Convert(y))); }\
  114. \
  115. void del(ktype x) { impl::del(Convert(x)); }\
  116. void del_item(dic_item it) { impl::del_item(impl::item(it)); }\
  117. ktype key(dic_item it) const { return ACCESS(ktype,impl::key(impl::item(it))); }\
  118. itype inf(dic_item it) const { return ACCESS(itype,impl::inf(impl::item(it)));}\
  119. \
  120. int size() const { return impl::size(); }\
  121. bool empty() const { return (size()==0) ? true : false; }\
  122. void clear() { impl::clear(); }\
  123. dic_item first_item() const { return dic_item(impl::first_item()); }\
  124. dic_item next_item(dic_item it) const { return dic_item(impl::next_item(impl::item(it))); }\
  125. \
  126. _dict_(ktype,itype,impl)& operator=(const _dict_(ktype,itype,impl)& D)\
  127. { impl::operator=(D); return *this; }\
  128. \
  129. _dict_class_(ktype,itype,impl)() {}\
  130. _dict_class_(ktype,itype,impl)(const _dict_(ktype,itype,impl)& D) : impl(D)\
  131. {}\
  132. ~_dict_class_(ktype,itype,impl)() { impl::clear(); }
  133.  
  134. #if defined(__TEMPLATE_ARGS_AS_BASE__)
  135. #define _dict_class_(a,b,c) _dictionary
  136. #define _dict_(a,b,c) _dictionary<a,b,c>
  137. template <class ktype, class itype, class impl> 
  138. _dictionary_class(ktype,itype,impl)
  139. };
  140. #else
  141. #define _dictionary(a,b,c)         name4(a,b,c,_dictionary)
  142. #define _dict_class_(a,b,c)        name4(a,b,c,_dictionary)
  143. #define _dict_(a,b,c)              name4(a,b,c,_dictionary)
  144. #define _dictionarydeclare3(_a,_b,_c) _dictionary_class(_a,_b,_c) };
  145. #endif
  146.  
  147.  
  148. #endif
  149.